package com.haoshuang.zhouzhoubang.ui.adapter

import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.databinding.ItemMemberGroupBinding
import com.haoshuang.zhouzhoubang.model.bean.TeamGroupBean
import com.haoshuang.zhouzhoubang.ui.activity.PersonalHomeActivity
import com.haoshuang.zhouzhoubang.ui.vm.MemberGroupViewModel
import com.lnkj.jialubao.utils.AccountUtils
import com.mufeng.libs.base.BaseBindingHolder
import com.mufeng.libs.base.BaseBindingQuickAdapter
import com.mufeng.libs.core.setOnItemChildClick
import com.mufeng.libs.utils.callPhone
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.vertical
import com.mufeng.libs.utils.view.visibleOrGone

/**
 * 
 */
class MemberGroupAdapter(data: MutableList<TeamGroupBean>, private val vm: MemberGroupViewModel) : BaseBindingQuickAdapter<TeamGroupBean, ItemMemberGroupBinding>(data) {

    override fun onBindViewHolder(holder: BaseBindingHolder, position: Int, item: TeamGroupBean?) {
        if (item == null) return
        holder.getViewBinding<ItemMemberGroupBinding>().apply {
            ivArrow.isSelected = item.isShow
            recyclerView.visibleOrGone(item.isShow && (item.member_count?:0) > 0)

            tvGroupName.text = item.group_name
            tvNum.text = "${item.member_count}人"

            llGroup.clickWithTrigger {
                item.isShow = !item.isShow
                notifyDataSetChanged()
                if (item.isShow) {
                    AccountUtils.addTeamGroupShowList(item.id.toString())
                }else {
                    AccountUtils.delTeamGroupShowList(item.id.toString())
                }
            }

            val adapter = MemberGroupUserAdapter(item.member_list as MutableList<TeamGroupBean.Member>)
            recyclerView.vertical()
                .adapter = adapter

            adapter.setOnItemClickListener { _, view, position ->
                val i = adapter.getItem(position)
                context.startPage<PersonalHomeActivity>("user_id" to item.uid)
            }

            adapter.setOnItemChildClick(id = R.id.tvSde){view, position, item ->
                if (item?.phone?.isBlank() == true) {
                    toast("电话不能为空")
                }
                context.callPhone(item?.phone.toString())
            }
        }
    }
}
